<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Google
{
	private $google_client_id;
	private $google_client_secret;
	private $google_redirect_url;
	private $google_developer_key;
	private $google_app_name;
	
    public function __construct()
    {
        /*if (!isset($this->CI))
        {
            $this->CI =& get_instance();
        }*/
		$this->google_client_id=CLIENT_ID;
		$this->google_client_secret=CLIENT_SECRET;
		$this->google_redirect_url=REDIRECT_URL;
		$this->google_developer_key=DEV_KEY;
		$this->google_app_name=SET_APP_NAME;
	}
	// thiet lap cac giatri cho app
	public function setGoClent(){
		//include google api files
		require_once APPPATH.'libraries/libgoogle/Google_Client.php';
		require_once APPPATH.'libraries/libgoogle/contrib/Google_Oauth2Service.php';
		
		$gClient = new Google_Client();
		$gClient->setApplicationName($this->google_app_name);
		$gClient->setClientId($this->google_client_id);
		$gClient->setClientSecret($this->google_client_secret);
		$gClient->setRedirectUri($this->google_redirect_url);
		$gClient->setDeveloperKey($this->google_developer_key);
		return $gClient;
	}
	// xu ly login 
	public function OauthService(){
		$gClient=$this->setGoClent();
		$google_oauthV2 = new Google_Oauth2Service($gClient);
		//If code is empty, redirect user to google authentication page for code.
		// get param code from button login
		if (isset($_GET['code'])) 
		{ 
			$gClient->authenticate($_GET['code']);
			$_SESSION['token'] = $gClient->getAccessToken();
			header('Location: ' . filter_var($this->google_redirect_url, FILTER_SANITIZE_URL));
			return;
		}
		//Code is required to aquire Access Token from google
		//Once we have access token, assign token to session variable
		//and we can redirect user back to page and login.
		if ($gClient->getAccessToken()) 
		{
			  $arrUser=array();	
			  //For logged in user, get details from google using access token
			  $user 						= $google_oauthV2->userinfo->get();
			  $arrUser['id'] 				= $user['id'];
			  $arrUser['name'] 				= filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
			  $arrUser['email'] 			= filter_var($user['email'], FILTER_SANITIZE_EMAIL);
			  $arrUser['url'] 				= filter_var($user['link'], FILTER_VALIDATE_URL);
			  $arrUser['image'] 			= filter_var($user['picture'], FILTER_VALIDATE_URL);
			  $arrUser['personMarkup'] 		= "$email<div><img src='$profile_image_url?sz=50'></div>";
			  $_SESSION['token'] 			= $gClient->getAccessToken();
			  return $arrUser;
		}
	}
	
	public function createAuthUrl(){
		//For Guest user, get google login url
		$gClient=$this->setGoClent();
		$authUrl = $gClient->createAuthUrl();
		return $authUrl;
	}
	
	public function Logout(){
		$gClient = new Google_Client();
		//If user wish to log out, we just unset Session variable
		if (isset($_REQUEST['reset'])) 
		{
		  unset($_SESSION['token']);
		  $gClient->revokeToken();
		  header('Location: ' . filter_var($this->google_redirect_url, FILTER_SANITIZE_URL)); //redirect user back to page
		}
	}
	// 
}